
.data
integerOption:  .asciiz "%d\n"

.text
####################### Funcion MCM ##############################
.ent mcm
mcm:
	.frame $30, 48, $31	# Frame fp, size, ra
        .set    noreorder
        .cpload $25		# cpload en t9
        .set    reorder
	subu	$29,$29,40
	.cprestore 16		# cprestore
	
	sw	$31,32($29)	#Saved Registers Area.
	sw	$30,28($29)
	sw	$28,24($29)
	move	$30,$29

	sw	$4,40($30)	#Argument building area.
	sw	$5,44($30)	#Argument building area.

	la $25,mcd
	jal $31,$25

	lw	$4,40($30)
	lw	$5,44($30)

	divu $2,$4,$2
	mul $2,$2,$5

	lw	$31,32($29)
	lw	$30,28($29)

	addu	$29,$29,48
	jr	$31
.end mcm

####################### Funcion MCD ##############################
.ent mcd
mcd:
	.frame $30, 40, $31	# Frame fp, size, ra
        .set    noreorder
        .cpload $25		# cpload en t9
        .set    reorder

	subu	$29,$29,40
	.cprestore 16		# cprestore
	
	sw	$31,32($29)	#Saved Registers Area.
	sw	$30,28($29)
	sw	$28,24($29)
	move	$30,$29

	sw	$4,40($fp)	#Argument building area.

	beqz $5,return
	remu $4,$4,$5		# Guardo el resto en $5
	move $6,$4		# Intercambio $4 y $5
	move $4,$5
	move $5,$6
	la $25,mcd
	jal $31,$25	
	
return:
	move $2,$4 

unwinding:	#Stack unwinding	
	lw	$31,32($29)
	lw	$30,28($29)

	addu	$29,$29,40
	jr	$31

.end mcd	

####################### Funcion main ##############################
.globl main
.ent main
main:
	.frame $30, 40, $31	# Frame fp, size, ra
        .set    noreorder
        .cpload $25		# cpload en t9
        .set    reorder

	subu	$29,$29,40	# creo stack frame
	.cprestore 16		# cprestore
	sw	$31,32($29)	# guardo la ra en 32(sp) 
	sw	$30,28($29)	# guardo el fp en 28(sp)
	sw	$28,24($29)	# guardo el gp en 24(sp)
	move	$30,$29		# move fp,sp

	li	$4,2366
	li	$5,273
	la	$25,mcd
	jal	$31,$25

	la	$4,integerOption
	move	$5,$2
	la	$25,printf
	jal	$31,$25

	li	$4,2366
	li	$5,273
	la	$25,mcm
	jal	$31,$25

	la	$4,integerOption
	move	$5,$2
	la	$25,printf
	jal	$31,$25
	
	move	$2,$0		# return 0

	move	$29,$30		# destruyo stack frame
	lw	$31,32($29)	# restauro ra
	lw	$30,28($29)	# restauro fp
	addu	$29,$29,40	# restauro la posicion del sp
	j	$31
	.end main
